package com.edu.test;/*
 *@Autor:yjy
 *@Date:2025/10/11:16:41:
 *
 */

import com.edu.bean.*;
import com.edu.mapper.*;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

public class Mytest {

    private SqlSession session;

    @Before
    public void init() throws IOException {
        String path = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(path);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        session = sessionFactory.openSession();
    }

    // 1. 查询指定id的学生和其所在的班级的信息(班级id和班级名字) - 使用嵌套结果
    @Test
    public void test01() {
        StudentMapper studentMapper = session.getMapper(StudentMapper.class);
        Student student = studentMapper.selectStudentWithClassById(1);
        System.out.println("学生信息: " + student);
        System.out.println("班级信息: " + student.getClassInfo());
    }

    // 2. 查询所有课程的信息和其上课老师的信息 - 使用嵌套查询
    @Test
    public void test02() {
        CourseMapper courseMapper = session.getMapper(CourseMapper.class);
        List<Course> courses = courseMapper.selectAllCoursesWithTeacher();
        for (Course course : courses) {
            System.out.println("课程信息: " + course);
            System.out.println("任课老师: " + course.getTeacher());
            System.out.println("----------");
        }
    }

    // 3. 查询指定成绩id的成绩信息和其学生信息以及课程信息 - 使用嵌套结果
    @Test
    public void test03() {
        ScoreMapper scoreMapper = session.getMapper(ScoreMapper.class);
        Score score = scoreMapper.selectScoreWithStudentAndCourseById(1);
        System.out.println("成绩信息: " + score.getScore());
        System.out.println("学生信息: " + score.getStudent());
        System.out.println("课程信息: " + score.getCourse());
    }

    // 4. 查询指定成绩id的成绩信息和其学生信息以及课程信息
    //    以及学生所在的班级信息和该课程上课老师的信息 - 使用嵌套查询
    @Test
    public void test04() {
        ScoreMapper scoreMapper = session.getMapper(ScoreMapper.class);
        Score score = scoreMapper.selectScoreWithStudentAndCourseById(1);

        System.out.println("=== 成绩详情调试信息 ===");
        System.out.println("成绩ID: " + score.getScoreId());
        System.out.println("分数: " + score.getScore());
        System.out.println("学生ID: " + score.getStudentId());
        System.out.println("课程ID: " + score.getCourseId());
        System.out.println("学生对象: " + score.getStudent());
        System.out.println("课程对象: " + score.getCourse());

        if (score.getStudent() != null) {
            System.out.println("学生姓名: " + score.getStudent().getStudentName());
        } else {
            System.out.println("学生信息为null");
        }

        if (score.getCourse() != null) {
            System.out.println("课程名称: " + score.getCourse().getCourseName());
        } else {
            System.out.println("课程信息为null");
        }
    }
}
